#include <stdafx.h>
#include <XDraw.h>
#include <sstream>

XTextPack::XTextPack(XHash hash)
{
	Hash = hash;
	Font = NULL;
	TextBox.W = 500;
	TextBox.H = 200;
	Text = "";

	Color = XColor(255, 255, 255, 255);

	Translation.X = 0;
	Translation.Y = 0;

	Rotation = 0;
	Scale = 1;
	Width = 0;
	Height = 0;
}

XTextPack::~XTextPack()
{

}

void XTextPack::SetText(std::string text)
{
	Text = text;
	Width = 0;
	Height = 0;
	int width = 0;
	int height = 0;

	if(Font != NULL)
	{

		for( int i = 0; i < Text.length(); i++)
		{
			if( Text[ i ] == ' ')
			{
				width += Font->Space;
			}

			else if(Text[ i ] == '\n')
			{

				Height += Font->NewLine + height;
				height = 0;

				if(Width < width)
					Width = width;

				width = 0;
			
			}

			else 
			{
				unsigned int ascii = (unsigned char)Text[i];

				Width += Font->Clips[ascii].W+1;

				if(height < Font->Clips[ascii].H)
				height = Font->Clips[ascii].H;
			}
		}
	}

	Height += height;

	if(Width < width)
		Width = width;
}

void XTextPack::SetText(int text)
{
	std::ostringstream ss;
	ss << text;
	SetText(ss.str());
}

void XTextPack::SetTextBox(XRectangle rect)
{
	TextBox = rect;
}

void XTextPack::SetColor(XColor color)
{
	Color = color;
}

void XTextPack::SetTranslation(XVector translation)
{
	Translation = translation;
}

void XTextPack::SetRotation(float degree)
{
	Rotation = degree;
}

void XTextPack::SetScale(float scale)
{
	Scale = scale;
}

void XTextPack::SetFont(XFont* font)
{
	Font = font;
}

XRectangle XTextPack::GetTextBox()
{
	return TextBox;
}

std::string XTextPack::GetText()
{
	return Text;
}

XHash XTextPack::GetHash()
{
	return Hash;
}

XColor XTextPack::GetColor()
{
	return Color;
}

XVector XTextPack::GetTranslation()
{
	return Translation;
}

float XTextPack::GetRotation()
{
	return Rotation;
}

float XTextPack::GetScale()
{
	return Scale;
}

int XTextPack::GetWidth()
{
	return Width;
}

int XTextPack::GetHeight()
{
	return Height;
}